#include "service_ctrl_impl.h"
bool ServiceCtrlImpl::addDept(Department& dpm)
{
	char name[20];
	cout << "请输入新增加的部门名称：";
	get_str(name,20);
	for(int i=0;i<deptArr.size();i++)
	{
		if(0==strcmp(deptArr[i].getName(),name))
		{
			cout << "与之前定义过的部门重复,操作失败!" << endl;
			anykey_continue();
			return false;
		}
	}
	dpm.setName(name);
	dpm.setNum(0);
	dpm.setId(getDpmId());
	deptArr.push_back(dpm);
	cout << "操作成功!新部门" << dpm.getName() << "已被加入!" <<endl;
	anykey_continue();
	return true;
}
int ServiceCtrlImpl::delDept(int ipx)
{
	for(int i=0;i<deptArr.size();i++)
	{
		if(ipx == deptArr[i].getId())
		{
			deptArr.erase(deptArr.begin()+i);
			cout << "操作成功!" << endl;
			anykey_continue();
			return 1;
		}
	}
	cout << "操作失败!该部门不存在!" << endl;
	anykey_continue();
	return 0;
}
vector<Department>& ServiceCtrlImpl::listDept(void)
{
	return deptArr;
}
bool ServiceCtrlImpl::addEmp(int ipx,Employee& emp)
{
	for(int i=0;i<deptArr.size();i++)
	{
		if(ipx == deptArr[i].getId())
		{
			char name[20];
			int age;
			char sex;
			cout << "请输入" << deptArr[i].getName() << "新员工姓名:";
			get_str(name,20);
			cin.clear();
			cout << "请输入" << deptArr[i].getName() << "新员工性别:";
			cin >> sex;
			cin.clear();
			cout << "请输入" << deptArr[i].getName() << "新员工年龄:";
			cin >> age;
			cin.clear();
			emp.setName(name);
			emp.setSex(sex);
			emp.setAge(age);
			emp.setId(getEmpId());
			int num = deptArr[i].getNum();
			deptArr[i].setNum(num+1);
			deptArr[i].empArr.push_back(emp);
			cout << "操作成功!新员工" << emp.getName() << "已被加入!" << endl;
			anykey_continue();
			return true;
		}
	}
	cout << "操作失败!该部门不存在!" << endl;
	anykey_continue();
	return false;
}
bool ServiceCtrlImpl::delEmp(int ipx)
{
	for(int i=0;i<deptArr.size();i++)
	{
		if(ipx == deptArr[i].getId())
		{
			int id;
			cin.clear();
			cout << "请输入想删除员工的Id:";
			cin >> id;
			for(int j=0;j<deptArr[i].empArr.size();j++)
			{
				if(id == deptArr[i].empArr[j].getId())
				{
					deptArr[i].empArr.erase(deptArr[i].empArr.begin()+j);
					int num = deptArr[i].getNum();
					deptArr[i].setNum(num-1);
					cout << "操作成功!" << endl;
					anykey_continue();
					return true;
				}
			}
			cout << "操作失败!该员工不存在!" << endl;
			anykey_continue();
			return false;
		}
	}
	cout << "操作失败!该部门不存在!" << endl;
	anykey_continue();
	return false;
}
bool ServiceCtrlImpl::modEmp(int ipx,Employee& emp)
{
	for(int i=0;i<deptArr.size();i++)
	{
		if(ipx == deptArr[i].getId())
		{
			int id;
			cin.clear();
			cout << "请输入想修改员工的Id:";
			cin >> id;
			for(int j=0;j<deptArr[i].empArr.size();j++)
			{
				if(id == deptArr[i].empArr[j].getId())
				{
					vector<string> str;
					str.push_back("姓名");
					str.push_back("性别");
					str.push_back("年龄");
					str.push_back("取消");
					int cmd = -1;
					int cur = 0;
					while(cmd!=3)
					{
						cmd = -1;
						cin.clear();
						clear_screen();
						cout << "请选择修改方向:" << endl;
						switch(cmd=get_cmd(str))
						{
							case 0:
							{
								cout << "请输入修改后的姓名:";
								char name[20];
								get_str(name,20);
								deptArr[i].empArr[j].setName(name);
								cout << "修改姓名成功!" << endl;
								anykey_continue();
							} break;
							case 1:
							{
								cout << "请输入修改后的性别:";
								char sex;
								cin >> sex;
								deptArr[i].empArr[j].setSex(sex);
								cout << "修改性别成功!" << endl;
								anykey_continue();
							} break;
							case 2:
							{
								cout << "请输入修改后的年龄:";
								int age;
								cin.clear();
								cin >> age;
								deptArr[i].empArr[j].setAge(age);
								cout << "修改年龄成功!" << endl;
								anykey_continue();
							} break;
							case 3:
							{
								cout << "终止修改!" << endl;
							} break;
							default: break;
						}
					}
					cout << "操作成功!" << endl;
					anykey_continue();
					return true;
				}
			}
			cout << "操作失败!该员工不存在!" << endl;
			anykey_continue();
			return false;
		}
	}
	cout << "操作失败!该部门不存在!" << endl;
	anykey_continue();
	return false;
}
Department* ServiceCtrlImpl::listEmp(int ipx)
{
	for(int i=0;i<deptArr.size();i++)
	{
		if(ipx == deptArr[i].getId())
		{
			return &deptArr[i];
		}
	}
	return NULL;
}
vector<Department>& ServiceCtrlImpl::listAllEmp()
{
	return deptArr;
}
